﻿
if (typeof Pui == 'undefined' || !Pui) {
    (function ($) {
        var Pui = window.Pui = {
            init: function () {
                baseurl = 'http://video.pomoho.com/do';
                _user = {
                    uid: 0,
                    nick: '',
                    spacename: '',
                    newmsg: 0
                };
                _is_login = false;
                document.write('<style type="text/css">.is_login{display:none}</style>');
                var n = this.cookie('LoginInfo');
                if (n) {
                    var arr = n.split('$***$');
                    _user = {
                        uid: arr[0].replace('LoginInfo=', ''),
                        nick: arr[1],
                        spacename: arr[2],
                        username: arr[3],
                        newmsg: 0
                    };
                    _is_login = true;
                    if (Pui.cookie('NewMsgCount' + _user.uid)) {
                        _user.newmsg = Pui.cookie('NewMsgCount' + _user.uid);
                        msg = 'unread';
                    } else {
                        msg = (_user.newmsg == 0) ? 'empty' : 'unread';
                    }
                }
            },
            cookie: function (name, value, options) {
                if (typeof (value) != 'undefined') {
                    options = options || {};
                    if (value === null) {
                        value = '';
                        options.expires = -1;
                    }
                    var expires = '';
                    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                        var date;
                        if (typeof options.expires == 'number') {
                            date = new Date();
                            date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                        } else {
                            date = options.expires;
                        }
                        expires = '; expires=' + date.toUTCString();
                    }
                    var path = options.path ? '; path=' + options.path : '';
                    var domain = options.domain ? '; domain=' + options.domain : '';
                    var secure = options.secure ? '; secure' : '';
                    document.cookie = [name, '=', name + '=' + value, expires, path, domain, secure].join('');
                } else {
                    var cookieValue = null;
                    if (document.cookie && document.cookie != '') {
                        var cookies = document.cookie.split(';');
                        for (var i = 0; i < cookies.length; i++) {
                            var cookie = jQuery.trim(cookies[i]);
                            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                                cookieValue = unescape(cookie.substring(name.length + 1));
                                break;
                            }
                        }
                    }
                    return cookieValue;
                }
            },
            Resize: function (im, nWidth, nHeight) {
                var obj = im || document.getElementById(im);
                var w, h, p1, p2;
                p1 = nWidth / nHeight;
                p2 = obj.width / obj.height;
                w = 0;
                h = 0;
                if (p1 < p2) {
                    w = nWidth;
                    h = nWidth * (1 / p2);
                } else {
                    if (obj.width < nWidth && obj.height < nHeight) {
                        w = obj.width;
                        h = obj.height;
                    } else {
                        h = nHeight;
                        w = nHeight * p2;
                    }
                }
                obj.width = w;
                obj.height = h;
                obj.style.marginTop = (nHeight - h) / 2 + 'px';
            },
            autoThumb: function (path, w, h) {
                var _self = this;
                $(path).each(function (i) {
                    var preldimg = new Image();
                    preldimg.onload = function () {
                        setTimeout(function () {
                            _self.Resize($(path)[i], w, h);
                            preldimg = null;
                        },
                        50);
                    }
                    preldimg.src = $(path)[i].src;
                });
            }
        }
        Pui.init();
    })(jQuery);
}
Pui.Site = {
    init: function () {
        Pui.newHeader.init();
        this.focusTurn('slide', { 'mode': 'simple', 'distance': -960, 'width': 950, 'height': 330, 'speed': 300, 'timer': 3000 });

        if (document.body.id == "index") {
            $("#" + tname).attr("class", "ac");
        } else {
            $("#" + tname).attr("class", "ac");
            if (document.body.id == "list" || document.body.id == "info") {
                if ($("#" + treg).attr("class") == undefined || $("#" + treg).attr("class") == '') {
                    if ($("#" + tclass).attr("class") != undefined && $("#" + tclass).attr("class") != '') {
                        $("#" + tclass).attr("class", "item ac");
                    } else {
                        $("#" + tname).attr("class", "item ac");
                    }
                } else {
                    $("#" + treg).attr("class", "item ac");
                }
            }
        }
        $(".box .row a").click(function () {
            var type = $(this).attr("type");
            if (document.body.id == "index") {
                if (type == 1) { //按类型
                    window.open("http://" + tname + ".baomihua.com/list/" + $(this).attr("id") + "_0_0_1.html");
                } else if (type == 2) { //按地区
                    window.open("http://" + tname + ".baomihua.com/list/0_" + $(this).attr("id") + "_0_1.html");
                } else if (type == 3) { //按时间
                    window.open("http://" + tname + ".baomihua.com/list/0_0_" + $(this).attr("id") + "_1.html");
                }
            }
            else {
                if (type == 1) { //按类型
                    $("#hidclassid").val($(this).attr("id"));
                    window.location.href = "http://" + tname + ".baomihua.com/list/" + $(this).attr("id") + "_" + $("#hidregionid").val() + "_" + $("#hidtime").val() + "_1.html";
                } else if (type == 2) { //按地区
                    $("#hidregionid").val($(this).attr("id"));
                    window.location.href = "http://" + tname + ".baomihua.com/list/" + $("#hidclassid").val() + "_" + $(this).attr("id") + "_" + $("#hidtime").val() + "_1.html";
                } else if (type == 3) { //按时间
                    $("#hidtime").val($(this).attr("id"));
                    window.location.href = "http://" + tname + ".baomihua.com/list/" + $("#hidclassid").val() + "_" + $("#hidregionid").val() + "_" + $(this).attr("id") + "_1.html";
                }
            }
        });
    },
    focusTurn: function (sAct, oData) {
        var i = 0, f = $('.picshow'), l = f.find('.current_pic .focus').length, s = '';
        function Title() {
            $('.picshow .title').find('a').removeClass('ac').hover(function () {
                clearTimeout(t);
                var _self = $(this);
                _self.addClass('ac');
                i = f.find('.title a').index(_self[0]);
                g(sAct, oData);
                return false;
            }, function () { }).click(function () { return false; });
            $('.current_pic').hover(function () {
                clearTimeout(t);
            });
        }
        function g(sAct, oData) {
            f.find('.title a').removeClass('ac');
            if (i < l) {
                switch (sAct) {
                    case 'slide':
                        f.find('.current_pic .row').animate({ left: oData.distance * i }, oData.speed);
                }
                f.find('.title a:eq(' + i + ')').addClass('ac');
                Pui.autoThumb('.picshow .current_pic .focus:eq(' + i + ') img', oData.width, oData.height);
                i++;
                t = setTimeout(function () { g(sAct, oData) }, oData.timer);
            }
            else { i = 0; g(sAct, oData); }
        }
        if (l) { f.find('.current_pic .row').width(oData.width * (l + 1)); Title(); g(sAct, oData); }
    }
}
Pui.newHeader = {
    init: function () {
        $('#SearchForm').submit(function () {
            Pui.newHeader.DoSearch(this);
            return false;
        });
        this.ChangeItem();
    },
    DoSearch: function (o) {
        var val = $(o).find('#SearchInput').val();

        var thisBody = $('body');
        var url;

        if ($.trim(val) == '' || val == '搜一下') {
            alert('对不起，关键词不能为空，请重新输入');
            $('#SearchInput').focus();
            return false;
        } else {
            var sdi = o.searchDomain;
            var sd = "http://so.baomihua.com";
            if (sdi != undefined && sdi.value != "") {
                sd = sdi.value;
            }
            var key = val;
            if (thisBody.hasClass('bBlog') || thisBody.hasClass('soBlog')) {
                url = sd + "/b/" + key + '.html';
            } else if (thisBody.hasClass('bImg') || thisBody.hasClass('soPic')) {
                url = sd + "/img/" + key + '.html';
            } else {
                url = sd + "/v/" + key + '_p0s04.html';
            }

            if (document.location.hostname.indexOf("so.baomihua.com") != -1) {
                document.location.href = url;
            } else {
                window.open(url, 'search');
            }
            return false;
        }
    },
    ChangeItem: function () {
        var thisBody = $('body');
        var yuanj = $('<b class="lt"></b><b class="rt"></b>');
        if (thisBody.hasClass('bBlog') || thisBody.hasClass('soBlog')) {
            $('#Item .sBlog').addClass('ac').append(yuanj);
            $('#Item a').not('.sBlog').removeClass('ac');
        } else if (thisBody.hasClass('bImg') || thisBody.hasClass('soPic')) {
            $('#Item .sPic').addClass('ac').append(yuanj);
            $('#Item a').not('.sPic').removeClass('ac');
        } else {
            $('#Item .sVideo').addClass('ac').append(yuanj);
            $('#Item a').not('.sVideo').removeClass('ac');
        };
    }
}

function sKeyWord() {
    var sInput = document.getElementById("SearchInput");
    sInput.value = SEARCH_KEYWORD;
    sInput.onfocus = function () {
        if (this.value == SEARCH_KEYWORD) {
            this.value = "";
        }
    }
    sInput.onblur = function () {
        if (this.value == "") {
            this.value = SEARCH_KEYWORD;
        }
    }
}
if (typeof (SEARCH_KEYWORD) != "undefined" && document.getElementById("SearchInput")) {
    sKeyWord();
}
Pui.space = {
    init: function () {
        this.VideoWelter()
    },
    playlist: function (a) {
        var b = a + 1,
        cPage = $("#VideoBox .row").length,
        rPage = 1 + cPage,
        lPage = -1,
        prevFlag = true,
        isLoding = false,
        nextFlag = true;
        $("#ListSwitch .r").addClass("r_b").attr("title", "向右翻");
        $("#ListSwitch .l").addClass("l_b").attr("title", "向左翻");
        $("#ListSwitch .playlist").find(".l").click(function () {
            if (isLoding) return false;
            b < 2 && prevFlag ? loadPage(false) : showPrev();
            return false
        });
        $("#ListSwitch .playlist").find(".r").click(function () {
            if (isLoding) return false;
            b >= cPage && nextFlag ? loadPage(true) : showNext();
            return false
        });
        var loadPage = function (flag) {
            var pageIndex = 0;
            var show = null;
            pageIndex = flag ? rPage++ : lPage--;
            isLoding = true;
            var rURL = "http://tv.baomihua.com/interfaces/playlist.ashx?jsoncallback=?&sid=" + subjcetid + "&row=" + curRow + "&page=" + pageIndex;
            $.getJSON(rURL,
            function (data) {

                if (data != null) {
                    show = showNextOrPrev(data);
                    var showIsNull = (show != null);
                    data = data;
                    if (data == null || data.UserVideos == null || data.UserVideos.length == 0) data = false;
                    else {
                        for (var o = "<div class='row'>",
                        r = {},
                        w = 0; w < data.UserVideos.length; w++) {

                            r = data.UserVideos[w];
                            o += "<ul>";
                            o += '<li class="vimg">';
                            if (sType == "0") {
                                o += '<a href="http://' + tname + '.baomihua.com/play/' + r.VideoId + '.html">';
                            }
                            else {
                                o += '<a href="http://cu.' + "365dianying" + '.com/play/' + r.VideoId + '.html">';
                            }
                            o += '<img src="' + r.Pic + '" title="' + r.Title + '" alt="' + r.Title + '"></a>';
                            o += "</li>";
                            o += '<li class="vtitle">';
                            if (sType == "0") {
                                o += '<a title="' + r.Title + '" href="http://' + tname + '.baomihua.com/play/' + r.VideoId + '.html">' + r.Title + "</a>";
                            }
                            else {
                                o += '<a title="' + r.Title + '" href="http://cu.' + "365dianying" + '.com/play/' + r.VideoId + '.html">' + r.Title + "</a>";
                            }

                            o += "</li>";
                            o += "</ul>"
                        }
                        o += "</div>";
                        data.PageIndex > 0 ? $("#VideoBox").append(o) : $("#VideoBox").prepend(o);
                        data = true
                    }
                    if (showIsNull & data) {
                        cPage = $("#VideoBox .row").length;
                        show()
                    }
                }
                isLoding = false;
            });
            return false
        },
        showNextOrPrev = function (data) {
            if (data == null) return null;
            var flag = (data.PageSize == data.PageCount);
            if (data.PageIndex > 0) {
                nextFlag = flag;
                return function () {
                    showNext()
                }
            } else {
                prevFlag = flag;
                return function () {
                    b++;
                    showPrev()
                }
            }
        },
        appendVideo = function (k) {
            var i = $("#VideoBox div.row");
            if (!(k && b - 2 > 0 && b < i.length - 1)) {
                var l = k ? i.length - 1 : 0;
                l = i.eq(l).find("ul");
                /*
                if (l.length != 9) {
                var g = 9 - l.length;
                l = k ? i.length - 2 : 1;
                l = $("#VideoBox div.row:eq(" + l + ") ul");
                if (k) {
                k = $("#VideoBox div.row:last");
                for (i = 0; i < g; i++) l.eq(i).clone().removeClass().prependTo(k)
                } else {
                k = $("#VideoBox div.row:first");
                for (i = 0; i < g; i++) l.eq(i).clone().removeClass().appendTo(k)
                }
                }*/
            }
        },
        enableBtn = function () {
            !prevFlag && b == 1 ? $("#ListSwitch .l").removeClass("l_b").attr("title", "左边没有了") : $("#ListSwitch .l").addClass("l_b").attr("title", "向左翻");
            !nextFlag && $("#VideoBox div.row").length == b ? $("#ListSwitch .r").removeClass("r_b").attr("title", "右边没有了") : $("#ListSwitch .r").addClass("r_b").attr("title", "向右翻");
            return false
        },
        showNext = function () {
            if (b <= cPage - 1) {
                appendVideo(true);
                $("#VideoBox").animate({
                    left: -(900 * b)
                },
                400);
                b++
            }
            enableBtn();
            return false
        },
        showPrev = function () {
            if (b >= 2) {
                appendVideo(false);
                $("#VideoBox").animate({
                    left: -(900 * (b - 2))
                },
                500);
                b--
            }
            enableBtn();
            return false
        }; (function () {
            var k = "";
            if ($("#VideoBox ul[id='current_" + flvid + "']").length > 0) for (var i = $("#VideoBox ul"), l = false, g = {},
            u = 0; u < i.length; u++) {
                g = i.eq(u);
                if (l) {
                    k = g.find("a").attr("href");
                    break
                }
                l = g.attr("id") != "" ? true : false
            }
            if (k == "") {
                if ($("#VideoBox ul:first a:first").length == 0 || $("#VideoBox ul:first a:first").length == 1) return;
                k = $("#VideoBox ul:first a:first").attr("href")
            }
            window.playNext = function () {
                location.href = k
            }
        })();
    },
    VideoWelter: function () {

        var a = $(".playlist #VideoBox");
        _self = this;
        rowN = $(".playlist #VideoBox .row").length;
        var b = "current_" + flvid,
        c = false;
        a.find(".row ul").each(function () {
            var d = $(this);
            if (d.attr("id") == b) {
                d.addClass("ac");
                d = a.find(".row").index(d.parent()[0]);
                a.animate({
                    left: -(900 * d)
                },
                500);
                _self.playlist(d);
                c = true
            }
        });
        c || _self.playlist(0)

    }
};


$('.ad_common').each(function() {
	var txt = $(this).html();
	if($.trim(txt) == ""){$(this).css("marginBottom","0px");}
});